package org.code.boot.controller.common;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import java.util.Map;
import java.util.Collections;
import cn.hutool.core.collection.CollUtil;
import org.code.boot.common.utils.CollectionUtils;
import org.code.boot.common.utils.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.code.boot.common.annotation.Log;
import org.code.boot.common.enums.BusinessType;
import org.code.boot.controller.BaseController;
import org.code.boot.vo.AjaxResult;
import org.code.boot.domain.common.ComMessage;
import org.code.boot.service.common.IComMessageService;
import org.code.boot.vo.TableDataInfo;
import org.code.boot.domain.system.SysUser;
import org.code.boot.service.system.ISysUserService;

/**
 * 留言Controller
 * 
 * @author coder
 * @date 2025-08-27
 */
@RestController
@RequestMapping("/common/message")
public class ComMessageController extends BaseController
{
    @Autowired
    private IComMessageService comMessageService;

    @Autowired
    private ISysUserService sysuserService;

    /**
     * 查询留言列表
     */
    @PreAuthorize("@ss.hasPermi('common:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComMessage comMessage)
    {
        startPage();
        List<ComMessage> list = comMessageService.selectComMessageList(comMessage);
        list = buildDetailList(list);
        return getDataTable(list);
    }



    /**
     * 获取留言详细信息
     */
    @PreAuthorize("@ss.hasPermi('common:message:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        return success(comMessageService.selectComMessageByMessageId(messageId));
    }

    /**
     * 新增留言
     */
    @PreAuthorize("@ss.hasPermi('common:message:add')")
    @Log(title = "留言", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComMessage comMessage)
    {
        comMessage.setCreateBy(getNickname());
        return toAjax(comMessageService.insertComMessage(comMessage));
    }

    /**
     * 修改留言
     */
    @PreAuthorize("@ss.hasPermi('common:message:edit')")
    @Log(title = "留言", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComMessage comMessage)
    {
        comMessage.setUpdateBy(getNickname());
        return toAjax(comMessageService.updateComMessage(comMessage));
    }

    /**
     * 删除留言
     */
    @PreAuthorize("@ss.hasPermi('common:message:remove')")
    @Log(title = "留言", businessType = BusinessType.DELETE)
	@DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(comMessageService.deleteComMessageByMessageIds(messageIds));
    }

    /**
     * 构建详细信息列表
     * 
     * @param list 列表数据
     * @return 构建后的列表数据
     */
    private List<ComMessage> buildDetailList(List<ComMessage> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        
        // 获取用户Map
        Map<Long, SysUser> userIdMap = sysuserService.getSysUserMap(
                CollectionUtils.convertSet(list, ComMessage::getUserId));
        
        // 拼接结果
        list.forEach(item -> {
            // 拼接用户名称
            MapUtils.findAndThen(userIdMap, item.getUserId(), 
                userIdItem -> item.setUserIdName(userIdItem.getNickName()));
        });
        
        return list;
    }
}
